package com.xmb.wcapi.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xmb.wcapi.annotation.Log;
import com.xmb.wcapi.constant.R;
import com.xmb.wcapi.entity.SysMenu;
import com.xmb.wcapi.entity.SysRolePermission;
import com.xmb.wcapi.entity.SysUser;
import com.xmb.wcapi.service.ISysMenuService;
import com.xmb.wcapi.service.ISysRolePermissionService;

@Controller
@RequestMapping("/menu")
public class MenuController {

	@Autowired
	private ISysMenuService iSysMenuService;

	@Autowired
	private ISysRolePermissionService iSysRolePermissionService;
	
	@RequestMapping("/index")
	public String index() {
		return "menu/index";
	}
	
	@RequestMapping("/addHtml")
	public String addHtml(String id,Model model) {
		model.addAttribute("menuId", id);
		return "menu/add";
	}
	
	@RequestMapping("/editHtml")
	public String editHtml(String id,Model model) {
		SysMenu sysMenu = iSysMenuService.selectInfo(new QueryWrapper<SysMenu>().eq("id", id));
		model.addAttribute("menu", sysMenu);
		return "menu/edit";
	}
	
	@RequestMapping("/addBtnHtml")
	public String addBtnHtml(String id,Model model) {
		model.addAttribute("menuId", id);
		return "menu/add_btn";
	}
	
	@RequestMapping("/editBtnHtml")
	public String editBtnHtml(String id,Model model) {
		SysMenu sysMenu = iSysMenuService.selectInfo(new QueryWrapper<SysMenu>().eq("id", id));
		model.addAttribute("menu", sysMenu);
		return "menu/edit_btn";
	}
	
	@RequestMapping("/iconHtml")
	public String iconHtml() {
		return "menu/icon";
	}
	
	@RequestMapping("/menuList")
	@ResponseBody
	public Object menuList() {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("code", 0);
		resultMap.put("msg", "ok");
		List<SysMenu> list = iSysMenuService.selectInfoList(new QueryWrapper<SysMenu>().ne("menu_type", "PDA"));
		List<Map<String, Object>> list1 = new ArrayList<>();
		Map<String, Object> map = null;
		for (SysMenu sysMenu : list) {
			map = new HashMap<String, Object>();
			map.put("id", sysMenu.getId());
			if(StringUtils.isEmpty(sysMenu.getSupperId())) {
				map.put("pid", "-1");
			}else {
				map.put("pid", sysMenu.getSupperId());
			}
			map.put("menuName", sysMenu.getMenuName());
			map.put("menuType", sysMenu.getMenuType());
			map.put("menuUrl", sysMenu.getMenuUrl());
			map.put("menuIcon", sysMenu.getMenuIcon());
			list1.add(map);
		}
		resultMap.put("data", list1);
		resultMap.put("count", list.size());
		return resultMap;
	}

	@RequestMapping("/openMenu")
	@ResponseBody
	public Object openMenu(String id) {
		//获取当前用户
		SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
		//根据当前用户角色查询拥有权限
		List<SysRolePermission> permissionList = iSysRolePermissionService.selectInfoList(new QueryWrapper<SysRolePermission>().eq("role_id", user.getRoleId()));
		Set<String> permissionIds = new HashSet<String>();
		for (SysRolePermission sysRolePermission : permissionList) {
			permissionIds.add(sysRolePermission.getPermissionId());
		}
		QueryWrapper<SysMenu> ew = new QueryWrapper<SysMenu>();
		ew.eq("supper_id", id);
		ew.in("id", permissionIds);
		if(permissionIds.size()==0) {
			ew.select("1!=1");
		}
		List<SysMenu> menuList = iSysMenuService.list(ew.orderByAsc("order_by_index"));
		return menuList;
	}
	
	@Log("添加菜单")
	@RequestMapping("/add")
	@ResponseBody
	public R add(SysMenu sysMenu) {
		try {
			if(StringUtils.isNotEmpty(sysMenu.getOrderByIndex())) {
				SysMenu menu = iSysMenuService.selectInfo(new QueryWrapper<SysMenu>().eq("supper_id", sysMenu.getSupperId())
						.eq("order_by_index", sysMenu.getOrderByIndex()));
				if(menu != null) {
					return R.error("排序的序号已存在，请勿重复添加");
				}
			}
			iSysMenuService.addInfo(sysMenu);
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("系统异常");
		}
		return R.ok();
	}
	
	@Log("修改菜单")
	@RequestMapping("/edit")
	@ResponseBody
	public R edit(SysMenu sysMenu) {
		try {
			List<SysMenu> list = iSysMenuService.selectInfoList(new QueryWrapper<SysMenu>().eq("supper_id", sysMenu.getId()));
			if(list.size()>0 && StringUtils.isNotEmpty(sysMenu.getMenuUrl())) {
				for (int i=0;i<list.size();i++) {
					if("菜单".equals(list.get(i).getMenuType())) {
						return R.error("当前节点下有子节点，不能给当前节点添加url");
					}
				}
			}else if(list.size()>0 && StringUtils.isEmpty(sysMenu.getMenuUrl())) {
				for (int i=0;i<list.size();i++) {
					if("按钮".equals(list.get(i).getMenuType()) && StringUtils.isEmpty(sysMenu.getMenuUrl())) {
						return R.error("当前节点为页面节点，url不能为空");
					}
				}
			}
			if(StringUtils.isNotEmpty(sysMenu.getOrderByIndex())) {
				SysMenu menu = iSysMenuService.selectInfo(new QueryWrapper<SysMenu>().eq("supper_id", sysMenu.getSupperId())
						.eq("order_by_index", sysMenu.getOrderByIndex()));
				if(menu != null && !sysMenu.getOrderByIndex().equals(iSysMenuService.selectInfo(new QueryWrapper<SysMenu>().eq("id", sysMenu.getId())).getOrderByIndex())) {
					return R.error("排序的序号已存在");
				}
			}
			iSysMenuService.edit(sysMenu);
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("系统异常");
		}
		return R.ok();
	}
	
	@Log("修改按钮")
	@RequestMapping("/editBtn")
	@ResponseBody
	public R editBtn(String data,String id) {
		try {
			SysMenu sysMenu = JSON.parseObject(data, SysMenu.class);
			iSysMenuService.removeById(id);
			iSysMenuService.save(sysMenu);
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("系统异常");
		}
		return R.ok();
	}
}